/*
NoBuffer Notepad Copyright (C) 2015  Mahmoud Jaoune

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "settingsdialog.h"
#include "ui_settingsdialog.h"

settingsdialog::settingsdialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::settingsdialog)
{
    ui->setupUi(this);
    //Only allow numbers in blockSizeInput
    ui->blockSizeInput->setValidator(new QIntValidator(0, 90000000, this));

    //Load default settings
    QSettings settings("settings.ini", QSettings::IniFormat);
    settings.beginGroup("Options");
    ui->blockSizeInput->setText(settings.value("blockSize").toString());

    QFont tempFont;
    tempFont.fromString(settings.value("font").toString());

    if(!tempFont.toString().isEmpty()) globalFont = tempFont;
    settings.endGroup();
}

settingsdialog::~settingsdialog()
{
    delete ui;
}

void settingsdialog::on_cancelSettings_clicked()
{
    this->close();
}

/*
 * Saves specified settings into settings.ini
 */
void settingsdialog::on_saveSettings_clicked()
{
    QSettings settingsFile("settings.ini", QSettings::IniFormat);
    settingsFile.beginGroup("options");

    //Block Size
    settingsFile.setValue("blockSize", ui->blockSizeInput->text());

    //Font
    if(!globalFont.toString().isEmpty())
    {
        settingsFile.setValue("font", globalFont.toString());
    }

    //Font color
    if(globalColor.isValid())
    {
        settingsFile.setValue("fontColor", globalColor.name());
    }

    //Background color
    if(globalBGColor.isValid())
    {
        settingsFile.setValue("bgColor", globalBGColor.name());
    }

    settingsFile.endGroup();

    emit reloadSettings();
    this->close();
}

/*
 * Loads font from settings.ini (If exists) and then stores font selected in globalFont so that it
 * can be saved when the user clicks "Save"
 */
void settingsdialog::on_fontButton_clicked()
{
    bool status;
    QFont fontTemp;

    QSettings settings("settings.ini", QSettings::IniFormat);
    settings.beginGroup("Options");
    fontTemp.fromString(settings.value("font").toString());
    settings.endGroup();

    QFont IntFont = fontTemp;

    if(fontTemp.toString().isEmpty())
    {
        fontTemp = QFontDialog::getFont(&status, this);
    }
    else
    {
        fontTemp = QFontDialog::getFont(&status, IntFont, this);
    }

    if(status == true)
    {
        globalFont = fontTemp;
    }
}

void settingsdialog::on_fontColorSelect_clicked()
{
    QColor defaultColor;

    QSettings settings("settings.ini", QSettings::IniFormat);
    settings.beginGroup("Options");
    QString colorString = settings.value("fontColor").toString();
    settings.endGroup();

    if(QColor::isValidColor(colorString))
    {
        defaultColor.setNamedColor(colorString);
    }
    else
    {
        defaultColor.setNamedColor("#000000");
    }

    QColor fontColor = QColorDialog::getColor(defaultColor, this, "Choose font color", 0);

    if(!fontColor.isValid()) fontColor.setNamedColor("#000000");

    globalColor = fontColor;
}

void settingsdialog::on_bgColorSelect_clicked()
{
    QColor defaultBGColor;

    QSettings settings("settings.ini", QSettings::IniFormat);
    settings.beginGroup("Options");
    QString colorString = settings.value("bgColor").toString();
    settings.endGroup();

    if(QColor::isValidColor(colorString))
    {
        defaultBGColor.setNamedColor(colorString);
    }
    else
    {
        defaultBGColor.setNamedColor("#FFFFFF");
    }

    QColor BGColor = QColorDialog::getColor(defaultBGColor, this, "Choose background color", 0);

    if(!BGColor.isValid()) BGColor.setNamedColor("#FFFFFF");

    globalBGColor = BGColor;
}
